﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

public partial class Control_ucSeason : System.Web.UI.UserControl
{
    [Description("設定顯示名"), Category("MISAPP"), DefaultValue("設定顯示名")]
    public string Caption
    {
        set;
        get;
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void SetData(System.Data.DataTable p_dt, int p_TextFieldPOS, int p_ValueFieldPOS)
    {
        if (p_dt.Columns.Count == 1)
        {
            p_ValueFieldPOS = 0;
        }
        SetData(p_dt
                , p_dt.Columns[p_TextFieldPOS].ColumnName
                , p_dt.Columns[p_ValueFieldPOS].ColumnName);
    }

    public void SetData(System.Data.DataTable p_dt, string p_TextField, string p_ValueField)
    {
        ddlYear.DataTextField = p_TextField;
        ddlYear.DataValueField = p_ValueField;
        ddlYear.DataSource = p_dt;
        ddlYear.DataBind();
    }

    public MISAPP.MISInterface.iSelectList GetSelectList(string p_field, object p_obj)
    {
        MISAPP.MISInterface.iSelectList sl = (MISAPP.MISInterface.iSelectList)p_obj;
        sl.SetReverse(true);
        sl.SetField(p_field);
        sl.SetGroupBy(false);

        //string[] months = getMonths();
        //foreach (string ym in months)
        //{
        //    sl.AddToList(ym);
        //}
        
        //sl.AddToList(getMonths());
        sl.AddToList(GetSeasonString());

        return sl;       
    }

    protected string seasonString = string.Empty;
    /// <summary>
    /// 傳回2010Q1
    /// </summary>
    /// <returns></returns>
    public string GetSeasonString()
    {
        if (seasonString == string.Empty)
        {
            getMonths();
        }
        return seasonString;
    }

    protected string getMonths()
    {
        string months = string.Empty;
        seasonString = string.Empty;
        
        switch (ddlSeason.SelectedValue.ToString())
        { 
            case "1":
                months = "{0}01,{0}02,{0}03";
                seasonString = "{0}Q1";
                break;
            case "2":
                months = "{0}04,{0}05,{0}06";
                seasonString = "{0}Q2";
                break;
            case "3":
                months = "{0}07,{0}08,{0}09";
                seasonString = "{0}Q3";
                break;
            case "4":
                months = "{0}10,{0}11,{0}12";
                seasonString = "{0}Q4";
                break;
        }
        seasonString = string.Format(seasonString, ddlYear.SelectedValue.ToString());
        return string.Format(months,
            ddlYear.SelectedValue.ToString());
        //return string.Format(months, 
        //    ddlYear.SelectedValue.ToString()).Split(new char[]{','});
    }
}
